home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / x68000.arc / SOURCE.ARC / X68000.MOD < prev   
Text File  |  1986-03-05  |  6KB  |  229 lines

  1. MODULE X68000;
  2. (*------------------------------------------------------------------*)
  3. (*                                                                  *)
  4. (*                    MC68000 Cross Assembler                       *)
  5. (*            Copyright (c) 1985 by Brian R. Anderson               *)
  6. (*                                                                  *)
  7. (*   This program may be copied for personal, non-commercial use    *)
  8. (*   only, provided that the above copyright notice is included     *)
  9. (*   on all copies of the source code.  Copying for any other use   *)
  10. (*   without the consent of the author is prohibited.               *)  
  11. (*                                                                  *)
  12. (*------------------------------------------------------------------*)
  13.  
  14.    IMPORT Debug;
  15.  
  16.    FROM Terminal IMPORT
  17.       WriteString, WriteLn, ReadString;
  18.  
  19.    FROM FileSystem IMPORT
  20.       File, Response, Delete, Lookup, Reset, Close;
  21.  
  22.    FROM Strings IMPORT
  23.       CompareStr, Assign, Concat, Length;
  24.  
  25.    IMPORT Strings;   (* For Delete *)
  26.  
  27.    IMPORT ASCII;
  28.  
  29.    FROM LongNumbers IMPORT
  30.       LONG;
  31.  
  32.    FROM SymbolTable IMPORT
  33.       SortSymTab;
  34.  
  35.    FROM Parser IMPORT
  36.       TOKEN, OPERAND, STRING, LineCount, LineParts;
  37.  
  38.    FROM CodeGenerator IMPORT
  39.       LZero, AddrCnt, Pass2, BuildSymTable, AdvAddrCnt, GetObjectCode;
  40.  
  41.    FROM Listing IMPORT
  42.       StartListing, WriteListLine, WriteSymTab;
  43.  
  44.    FROM Srecord IMPORT
  45.       StartSrec, WriteSrecLine, EndSrec;
  46.  
  47.    FROM ErrorX68 IMPORT
  48.       ErrorCount, WriteErrorCount;
  49.  
  50.  
  51.    TYPE
  52.       FileName = ARRAY [0..14] OF CHAR;
  53.  
  54.  
  55.    VAR
  56.       SourceFN, ListFN, SrecFN : FileName;
  57.       Source, List, Srec : File;
  58.       Label, OpCode : TOKEN;
  59.       SrcOp, DestOp : OPERAND;
  60.       EndFile : BOOLEAN;
  61.       NumSyms : CARDINAL;
  62.       ObjOp, ObjSrc, ObjDest : LONG;
  63.       nA, nO, nS, nD : CARDINAL;
  64.  
  65.  
  66.  
  67.    PROCEDURE MakeNames (VAR S, L, R : FileName);
  68.    (* builds names for Source, Listing & S-Record files *)
  69.  
  70.       VAR
  71.          T : FileName;   (* temporary work name *)
  72.          i, l : CARDINAL;
  73.  
  74.       BEGIN
  75.          L := '';   R := '';   (* set Listing & S-rec names to null *)
  76.  
  77.          i := 0;   l := 0;
  78.          WHILE (S[i] # 0C) AND (S[i] # ' ') DO
  79.             IF S[i] = '.' THEN   (* mark beginning of file extension *)
  80.                l := i;
  81.             END;
  82.             S[i] := CAP (S[i]);
  83.             INC (i);
  84.          END;
  85.       
  86.          IF S[i] = ' ' THEN
  87.             Strings.Delete (S, i, Length (S) - i);
  88.          END;
  89.  
  90.          Assign (S, T);
  91.          IF l = 0 THEN
  92.             Concat (T, ".ASM", S);
  93.          ELSE   
  94.             Strings.Delete (T, l, i - l);
  95.          END;
  96.  
  97.          Concat (T, ".LST", L);
  98.          Concat (T, ".S", R);
  99.       END MakeNames;
  100.  
  101.  
  102.  
  103.    PROCEDURE OpenFiles;
  104.       BEGIN
  105.          Lookup (Source, SourceFN, FALSE);
  106.          IF Source.res # done THEN
  107.             WriteLn;
  108.             WriteString ("No Source File: ");   WriteString (SourceFN);   
  109.             WriteLn;
  110.             HALT;
  111.          END;
  112.  
  113.          Delete (ListFN, List);   (* Just in case file already exists *)
  114.          Lookup (List, ListFN, TRUE);   
  115.          IF List.res # done THEN    (* DOS may trap this *)
  116.             WriteLn;
  117.             WriteString ("Cannot create disk files!");   WriteLn;
  118.             HALT;
  119.          END;
  120.  
  121.          Delete (SrecFN, Srec);
  122.          Lookup (Srec, SrecFN, TRUE);
  123.          IF Srec.res # done THEN
  124.             WriteLn;
  125.             WriteString ("Cannot create disk files!");   WriteLn;
  126.             HALT;
  127.          END;
  128.       END OpenFiles;
  129.  
  130.  
  131.  
  132.    PROCEDURE StartPass2;
  133.       BEGIN
  134.          Reset (Source);
  135.          IF Source.res # done THEN
  136.             WriteString ("Unable to 'Reset' Source file for 2nd Pass.");
  137.             WriteLn;
  138.             HALT;
  139.          END;
  140.          Pass2 := TRUE;   (* Pass2 IMPORTed from CodeGenerator *)
  141.          AddrCnt := LZero;   (* Assume ORG = 0 to start *)
  142.          ErrorCount := 0;   (* ErrorCount IMPORTed from ErrorX68 *)
  143.          LineCount := 0;   (* LineCount IMPORTed from Parser *)
  144.          EndFile := FALSE;
  145.       END StartPass2;
  146.  
  147.  
  148.  
  149.    PROCEDURE CloseFiles;
  150.       BEGIN
  151.          Close (Source);
  152.          Close (List);
  153.          Close (Srec);
  154.          IF (Source.res # done) OR (List.res # done) OR (Srec.res # done) THEN
  155.             WriteString ("Error closing files...");   WriteLn;
  156.             HALT;
  157.          END;
  158.       END CloseFiles;
  159.  
  160.  
  161.  
  162. BEGIN   (* X68000 -- main program *)
  163.    WriteLn; 
  164.    WriteString ("Enter Source Filename: ");
  165.    ReadString (SourceFN);
  166.    WriteLn;
  167.  
  168.    MakeNames (SourceFN, ListFN, SrecFN);   
  169.  
  170.    OpenFiles;
  171.  
  172.    WriteLn;
  173.    WriteString ("                 68000 Cross Assembler");   WriteLn;
  174.    WriteString ("         Copyright (c) 1985 by Brian R. Anderson");
  175.    WriteLn;   WriteLn;
  176.    WriteString ("                 Assembling ");   WriteString (SourceFN);  
  177.    WriteLn;   WriteLn;   WriteLn;
  178.  
  179.  
  180. (*---
  181.     Begin Pass 1 
  182.                   ---*)
  183.    WriteString ("PASS 1");   WriteLn;
  184.    AddrCnt := LZero;   (* Assume ORG = 0 to start *)
  185.    EndFile := FALSE;
  186.  
  187.    REPEAT
  188.       LineParts (Source, EndFile, Label, OpCode, SrcOp, DestOp);
  189.       
  190.       BuildSymTable (AddrCnt, Label, OpCode, SrcOp, DestOp);
  191.  
  192.       AdvAddrCnt (AddrCnt);
  193.  
  194.    UNTIL EndFile OR (CompareStr (OpCode, "END") = 0);
  195.  
  196.  
  197. (*---
  198.    Begin Pass 2 
  199.                ---*)
  200.    WriteString ("PASS 2");   WriteLn;
  201.    StartPass2;   (* get Source file, Parser & ErrorX68 ready for 2nd pass *)
  202.    SortSymTab (NumSyms);
  203.    StartListing (List);
  204.    StartSrec (Srec, SourceFN);
  205.  
  206.    REPEAT
  207.       LineParts (Source, EndFile, Label, OpCode, SrcOp, DestOp);
  208.  
  209.       GetObjectCode (Label, OpCode,
  210.                      SrcOp,  DestOp, 
  211.                      AddrCnt, ObjOp, ObjSrc, ObjDest, 
  212.                      nA,      nO,    nS,     nD      );
  213.  
  214.       WriteListLine (List, AddrCnt, ObjOp, ObjSrc, ObjDest, nA, nO, nS, nD);
  215.  
  216.       WriteSrecLine (Srec, AddrCnt, ObjOp, ObjSrc, ObjDest, nA, nO, nS, nD); 
  217.       
  218.       AdvAddrCnt (AddrCnt);
  219.  
  220.    UNTIL EndFile OR (CompareStr (OpCode, "END") = 0);
  221.  
  222.    EndSrec (Srec);   (* Also: Finish off any partial line *)
  223.    WriteErrorCount (List);   (* Error count output to Console & Listing file *)
  224.    WriteSymTab (List, NumSyms);   (* Write Symbol Table to Listing File *)
  225.    CloseFiles;
  226. END X68000.
  227.  
  228.  
  229.